﻿<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>添加编辑用户</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
</head>
<body>
  <div class="layui-form" lay-filter="form-add" id="form-add" style="padding: 20px 30px 0 0;">
    <input type="hidden" name="id" value="" />
    <div class="layui-form-item">
      <label class="layui-form-label" style="width: 100px">登录名</label>
      <div class="layui-input-inline">
        <input type="text" name="username" lay-verify="required" style="width:300px;" value="" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label" style="width: 100px">登录密码</label>
      <div class="layui-input-inline">
        <input type="text" name="password2" style="width:300px;" autocomplete="off" value="" class="layui-input">
      </div>
      <div id="msg_password" style="display:none;" class="layui-form-mid layui-word-aux">留空不更改</div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label" style="width: 100px">用户姓名</label>
      <div class="layui-input-inline">
        <input type="text" name="realname" lay-verify="required" style="width:300px;" value="" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label" style="width: 100px">所属角色</label>
      <div class="layui-input-inline">
        <select name="roleid" id="roleid" lay-verify="required">
          <option value="">请选择角色</option>
        </select>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label" style="width: 100px">所属部门</label>
      <div class="layui-input-inline">
        <select name="departid" id="departid" lay-verify="required">
          <option value="">请选择部门</option>
        </select>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label" style="width: 100px">性别</label>
      <div class="layui-input-inline">
        <input type="radio" name="gender" value="男" title="男"/>
        <input type="radio" name="gender" value="女" title="女" />
        <input type="radio" name="gender" value="" title="未知" checked/>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label" style="width: 100px">手机号码</label>
      <div class="layui-input-inline">
        <input type="text" name="mobile" lay-verify="required" style="width:300px;" value="" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label" style="width: 100px">电子邮箱</label>
      <div class="layui-input-inline">
        <input type="text" name="email" style="width:300px;" value="" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label" style="width: 100px">身份证号</label>
      <div class="layui-input-inline">
        <input type="text" name="pid" style="width:300px;" value="" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label" style="width: 100px">用户标识</label>
      <div class="layui-input-inline">
        <input type="text" name="code" style="width:300px;" value="" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label" style="width: 100px">用户状态</label>
      <div class="layui-input-inline">
        <input type="checkbox" lay-verify="required" lay-filter="status" name="status" value="1" lay-skin="switch" lay-text="启用|禁用">
      </div>
    </div>
    <div class="layui-form-item layui-hide">
      <input type="button" lay-submit lay-filter="btn_submit" id="btn_submit" value="确定">
    </div>
  </div>
  <script src="/plugins/layui/layui.js"></script>
  <script>
  layui.config({
    base: '/plugins/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form'], function(){
    var $ = layui.$, form = layui.form, router = layui.router();
    var load_start = layer.load(2);
    $.ajax({
        type: "get",
        url: "user_add",
        data: router.search,
        dataType: "json",
        success: function (res) {
            layer.close(load_start);
            if (res.code == 0) {
                $.each(res.rolelist, function (i, v) {
                    $("#roleid").append("<option value='" + v.id + "'>" + v.name + "</option>");
                });
                $.each(res.departlist, function (i, v) {
                    $("#departid").append("<option value='" + v.id + "'>" + v.name + "</option>");
                });
                if (res.userinfo.id != null) {
                    $("#msg_password").show();
                }
                form.val("form-add", res.userinfo);
            }
            else {
                layer.alert(res.msg);
            }
        }
    });
    //监听提交
    form.on('submit(btn_submit)', function (data) {
      var load_index = layer.load(2);
      var field = data.field; //获取提交的字段
      var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引  
      $(this).addClass("layui-btn-disabled").prop("disabled" , true);
      //提交 Ajax 成功后，关闭当前弹层并重载表格
      $.ajax({
            type: "post",
            url: "user_add",
            data: field,
            dataType: "json",
            success: function (res) {
              if (res.code == 0)
              {
                layer.close(load_index);
                layer.msg(res.msg,{time:1*1000},function(){
                  parent.layui.table.reload('dds-list'); //重载表格
                  parent.layer.close(index); //再执行关闭 
                });
              }
              else
              {
                layer.close(load_index);
                layer.alert(res.msg)
              }
            }
      });
    });
  })
  </script>
</body>
</html>